package com.mjfeed.fragment;

import java.util.ArrayList;
import java.util.List;

import com.mjfeed.activity.CheckoutActivity;
import com.mjfeed.activity.MainActivity;
import com.mjfeed.activity.R;
import com.mjfeed.activity.RegisterLoginActivity;
import com.mjfeed.adapter.CartItemAdapter;
import com.mjfeed.bean.CartItemJSON;
import com.mjfeed.bean.CartItemListJSON;
import com.mjfeed.service.CartItemService;
import com.mjfeed.util.NetUtils;
import com.mjfeed.util.SPUtils;
import com.mjfeed.widget.LoadingDialog;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 购物车
 * @author zhaominglei
 * @date 2014-12-21
 * 
 */
public class ShoppingFragment extends BaseFragment implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = ShoppingFragment.class.getSimpleName();
	private View view;
	private MainActivity activity;
	
	private ImageView goHome;
	private RelativeLayout nologinReLayout;
	private RelativeLayout nologinhint;
	private Button loginBtn; //登录
	private Button shoppingBtn; //去秒杀
	private RelativeLayout loginReLayout;
	private LoadingDialog loadingDialog; //加载框
	private ListView cartItemListView;
	private List<CartItemJSON> cartItems = new ArrayList<CartItemJSON>();
	private CartItemAdapter cartItemAdapter;
	@SuppressWarnings("unused")
	private String referer; //来自那个链接
	private String username; //用户名
	private String sign; //签名
	private RelativeLayout totalCheckout;
	private TextView totalText; //总计
	private double total = 0.0d; //总计金额
	private Button checkoutBtn; //去结算
	private Intent intent;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_shopping, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		view = getView();
		activity = (MainActivity)getActivity();
		super.onActivityCreated(savedInstanceState);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) view.findViewById(R.id.shopping_gohome);
		nologinReLayout = (RelativeLayout) view.findViewById(R.id.shopping_relativelayout_nologin);
		nologinhint = (RelativeLayout) view.findViewById(R.id.shopping_nologin);
		loginBtn = (Button) view.findViewById(R.id.shopping_login);
		shoppingBtn = (Button) view.findViewById(R.id.shopping_shopping);
		loginReLayout = (RelativeLayout) view.findViewById(R.id.shopping_relativelayout_login);
		cartItemListView = (ListView) view.findViewById(R.id.shopping_cart_item_listview);
		totalCheckout = (RelativeLayout) view.findViewById(R.id.shopping_total_checkout);
		totalText = (TextView) view.findViewById(R.id.shopping_total);
		checkoutBtn = (Button) view.findViewById(R.id.shopping_checkout);
		
		goHome.setOnClickListener(this);
		loginBtn.setOnClickListener(this);
		shoppingBtn.setOnClickListener(this);
		checkoutBtn.setOnClickListener(this);
		
		nologinhint.setVisibility(View.VISIBLE);
		nologinReLayout.setVisibility(View.VISIBLE);
		loginReLayout.setVisibility(View.GONE);
		referer = activity.getIntent().getStringExtra("referer");
		username = (String)SPUtils.get(activity, "username", "");
		sign = (String)SPUtils.get(activity, "sign", "");
		if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
			nologinhint.setVisibility(View.GONE);
			nologinReLayout.setVisibility(View.GONE);
			loginReLayout.setVisibility(View.VISIBLE);
		} 
		cartItemAdapter = new CartItemAdapter(activity, cartItems);
		cartItemListView.setAdapter(cartItemAdapter);
		getCartItems();
	}
	
	private void getCartItems() {
		if (NetUtils.isConnected(activity)) {
			loadingDialog = new LoadingDialog(activity);
			loadingDialog.show();
			new CartItemAsyncTask().execute("");
		} else {
			Toast.makeText(activity, R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.shopping_gohome:
			activity.setFragmentIndicator(0);
			break;
		
		case R.id.shopping_login:
			intent = new Intent(activity, RegisterLoginActivity.class);
			intent.putExtra("referer", "ShoppingFragment");
			startActivity(intent);
			activity.finish();
			break;
			
		case R.id.shopping_shopping:
			activity.setFragmentIndicator(0);
			break;
		
		case R.id.shopping_checkout:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent = new Intent(activity, CheckoutActivity.class);
				intent.putExtra("referer", "ShoppingFragment");
				intent.putExtra("total", total);
				startActivity(intent);
				activity.finish();
			} else {
				intent = new Intent(activity, RegisterLoginActivity.class);
				intent.putExtra("referer", "ShoppingFragment");
				startActivity(intent);
				activity.finish();
			}
			break;
			
		default:
			break;
		}
	}

	public class CartItemAsyncTask extends AsyncTask<String, String, CartItemListJSON> {
		@Override
		protected CartItemListJSON doInBackground(String... params) {
			return new CartItemService().syncAndGetAll(activity);
		}
		@Override
		protected void onPostExecute(CartItemListJSON result) {
			loadingDialog.dismiss();
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				nologinReLayout.setVisibility(View.GONE);
				totalCheckout.setVisibility(View.VISIBLE);
				loginReLayout.setVisibility(View.VISIBLE);
				
				cartItemAdapter.setCartItems(result.getRecordList());
				cartItemAdapter.notifyDataSetChanged();
				List<CartItemJSON> recordList = result.getRecordList();
				for (CartItemJSON cartItemJSON : recordList) {
					total += cartItemJSON.getPrice().doubleValue()*cartItemJSON.getQuantity();
				}
				totalText.setText(String.valueOf(total));
			} else {
				nologinReLayout.setVisibility(View.VISIBLE);
				nologinhint.setVisibility(View.INVISIBLE);
				totalCheckout.setVisibility(View.INVISIBLE);
			}
		}
	}
}
